<?php

if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class Sell extends MY_Controller
{

    private $_type = array(
        'all',
        'personal',
        'agent',
    );

    /**
     * 首页加载出租信息
     * @param string $type 全部还是个人、中介
     * @param int $offset 游标
     */
    public function index( $type = 'all', $offset = 0 )
    {
        $data = array( );
        $this->get_common_section( $data );

        if ( !in_array( $type, $this->_type ) ) $type = 'all';
        
        //搜索
        $search_form = Modules::run( 'search' );
        $data['search_form'] = $search_form;

        //信息列表
        $info_list = Modules::run( 'info/sellGet', $type, $offset );
        $data['info_list'] = $info_list;

        $this->load->view( 'rent', $data );
    }

    /**
     * 按地区查询数据
     * @param int $zoneId 地区ID
     * @param string $type 类型
     * @param int $offset 游标
     */
    public function zone( $zoneId, $type = 'all', $offset = 0 )
    {
        $zoneId = intval( $zoneId );
        $zoneData = $this->model_common_getZoneDataById( $zoneId );
        if ( empty( $zoneData ) )
        {
            $data['search_form'] = '';
            //提示
            $messageData = array(
                'title' => '错误操作',
                'message' => '抱歉，该区域不存在',
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => config_item( 'base_url' ),
            );
            $messageBox = Modules::run( 'messageBox', $messageData );
            $data['info_list'] = $messageBox;
        }
        else
        {
            //搜索
            $search_form = Modules::run( 'search' );
            $data['search_form'] = $search_form;

            //信息列表
            $info_list = Modules::run( 'info/sellZone', $zoneId, $type, $offset );
            $data['info_list'] = $info_list;
        }

        $this->get_common_section( $data );
        $this->load->view( 'rent', $data );
    }

    /**
     * 根据厅室ID获取出租数据
     * @param int $roomId 厅室ID
     * @param string $type 类型
     * @param int $offset 游标
     */
    public function room( $roomId, $type = 'all', $offset = 0 )
    {
        $roomId = intval( $roomId );
        $roomData = $this->model_common_getRoomDataById( $roomId );
        if ( empty( $roomData ) )
        {
            $data['search_form'] = '';
            //提示
            $messageData = array(
                'title' => '错误操作',
                'message' => '抱歉，该类型不存在',
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => config_item( 'base_url' ),
            );
            $messageBox = Modules::run( 'messageBox', $messageData );
            $data['info_list'] = $messageBox;
        }
        else
        {
            //搜索
            $search_form = Modules::run( 'search' );
            $data['search_form'] = $search_form;

            //信息列表
            $info_list = Modules::run( 'info/sellRoom', $roomId, $type, $offset );
            $data['info_list'] = $info_list;
        }

        $this->get_common_section( $data );
        $this->load->view( 'rent', $data );
    }

    /**
     * 根据价格范围获取出租数据
     * @param string $range 价格范围
     * @param string $type 类型
     * @param int $offset 游标
     */
    public function price( $range, $type = 'all', $offset = 0 )
    {
        $rangeArr = explode( '-', $range );
        if ( count( $rangeArr ) == 2 )
        {
            $lowPrice = intval( $rangeArr[0] );
            $highPrice = ( $rangeArr[1] == 'max' ) ? 'max' : intval( $rangeArr[1] );
            $tmp = 0;
            if ( $highPrice != 'max' && $lowPrice > $highPrice )
            {
                $tmp = $lowPrice;
                $lowPrice = $highPrice;
                $highPrice = $tmp;
            }
        }
        else
        {
            $lowPrice = $highPrice = 0;
        }

        //搜索
        $search_form = Modules::run( 'search' );
        $data['search_form'] = $search_form;

        //信息列表
        $info_list = Modules::run( 'info/sellPrice', $lowPrice, $highPrice, $type, $offset );
        $data['info_list'] = $info_list;
        
        $this->get_common_section( $data );
        $this->load->view( 'rent', $data );
    }

}